<?php
/**
 * Created by PhpStorm.
 * User: songs
 * Date: 2019/10/12
 * Time: 10:13
 */

namespace App\admin\TableView;


use App\admin\TableView\BaseClass\BaseViewList;
use App\Jobs\Fun;
use App\Models\Admin\ApilistModel;
use App\Models\Admin\UserModel;

class UserListView extends BaseViewList
{

    public function get_model()
    {

        return new UserModel();
    }

    public $topTitle = '用户列表';
    public $PageSize = 10;

    public $breadcrumb = '<li><a href="/mcadmin">Home</a></li>
        <li><a href="javascript:;">用户列表</a></li>
        ';

    public $html = '';


    //  public $show_add = false; ///显示添加按钮

    /**
     * 获取表格的唯一名字
     */
    public function getName()
    {
        return 'UserModel';
    }


    /**
     * 需要继承,然后定义每行需要显示的数据格式
     */
    public function Labels()
    {
        return array(
            //   BaseViewList::Button_checkbox=>[],
            'id' => [
                'width' => 40
            ],
            'phone' => ['width' => 80],
            'name' => ['width' => 80],
            'money' => ['width' => 80],
            'created_at' => ['width' => 80],
            'is_company' => ['width' => 80],
            'is_factory' => ['width' => 80],
            'is_yingpin' => ['width' => 80],
        //    'orderDocking' => ['width' => 80],
            'notes' => ['width' => 80],
            BaseViewList::Button_control => [

                BaseViewList::Button_Edit,
                BaseViewList::Button_Delete

            ]
        );
    }

    /**
     * 对话框中的各个字段
     */
    public function dialog()
    {
        return array(
            array(
                'zd' => 'name'
            ),

            array(
                'zd' => 'head',
                'type' => self::dialog_Img,
            ),

            array(
                'zd' => 'notes',
                'type' => self::dialog_texthtml,
            ),
        );
    }

    /**
     * 方便继承,多一层过滤
     */
    public function ModelTohtml($key, $model)
    {

        if($key=='name'){
            return $this->toname($model);
        }
        if($key=='phone'){
            return $this->toPhone($model);
        }
        if($key=='created_at'){
            return Fun::date($model->created_at);
        }
        if($key=='is_company'||$key=='is_factory'||$key=='is_yingpin'){
            return $this->isOn($model->$key);
        }

        return parent::ModelTohtml($key, $model);
    }

    private function toPhone($model){
        return '<a title="点击查看详情" href="/mcadmin/basic/member/userinfo?id='.$model->id.'" target="_blank">'.$model->phone.'</a>';
    }


    private function isOn($val){
        if($val==0){
            return "未启用";
        }else{
            return '启用';
        }
    }

    private function toname($model){
        $html='<div class="toname">
                <img src="'.Fun::pImg($model->head?$model->head:\App\Models\User\UserModel::defaultHead).'" class="head"/>
                <span class="name">'.$model->name.'</span>
            </div>';
        return $html;
    }


    /**
     * 返回查询的条件
     */
    public function find()
    {
        $find = $this->get_model()->where([
        ]);
        $phone = $_GET['phone'] ?? '';
        if ($phone !== '') {
            $phone = '%' . $phone . '%';
            $find->whereRaw('phone LIKE ?', [
                $phone
            ]);
        }
        $name = $_GET['name'] ?? '';
        if ($name !== '') {
            $name = '%' . $name . '%';
            $find->whereRaw('name LIKE ?', [
                $name
            ]);
        }
        $is_company=$_GET['is_company']??'';
        if($is_company!==''){
            $find->whereRaw('is_company = ?', [
                $is_company
            ]);
        }
        $is_factory=$_GET['is_factory']??'';
        if($is_factory!==''){
            $find->whereRaw('is_factory = ?', [
                $is_factory
            ]);
        }
        $is_yingpin=$_GET['is_yingpin']??'';
        if($is_yingpin!==''){
            $find->whereRaw('is_yingpin = ?', [
                $is_yingpin
            ]);
        }

        $find->orderBy('id', 'DESC');
        return $find;
    }

    /**
     * 顶部的选择区,参数,如果为空,则不显示选择器
     */
    public function getselectAttr()
    {
        ///echo "<*>";
        ///例子
        $where = [
            'phone' =>
                [
                    'title' => '手机号',
                    'inputtype' => self::htmlText,
                ],
            'name' =>
                [
                    'title' => '昵称',
                    'inputtype' => self::htmlText,
                ],

            'is_company' =>
                [
                    'title' => '启用公司',
                    'inputtype' => self::dropDownList,
                    'items' => ['' => '所有', 1 => '启用',
                        0 => '关闭'],
                    'default' => ''
                ],

            'is_factory' =>
                [
                    'title' => '启用工厂',
                    'inputtype' => self::dropDownList,
                    'items' => ['' => '所有', 1 => '启用',
                        0 => '关闭'],
                    'default' => ''
                ],

            'is_yingpin' =>
                [
                    'title' => '启用应聘',
                    'inputtype' => self::dropDownList,
                    'items' => ['' => '所有', 1 => '启用',
                        0 => '关闭'],
                    'default' => ''
                ]
        ];
        return $where;
    }

}